<?php

namespace app\stock\controller;

use think\Controller;

class Admin extends Controller
{
  public function _initialize()
  {
    parent::_initialize();
    $this->AdminModel = model('Admin');
  }
  // 授权登录
  public function login()
  {
    $code = $this->request->param('code', '', 'trim');

    if (!$code) {
      $this->error('获取临时凭证失败');
    }

    $openid = $this->code2Session($code);

    if (!$openid) {
      $this->error('授权登陆失败');
    }

    $admininfo = $this->AdminModel->where('openid', $openid)->find();

    if (!$admininfo) {
      $this->success('未绑定帐号，请先去绑定');
    } else {
      $data = [
        'id' => $admininfo['id'],
        'username' => $admininfo['username'],
        'nickname' => $admininfo['nickname'],
        'avatar' => $admininfo['avatar_text'],
        'email' => $admininfo['email'],
        'mobile' => $admininfo['mobile'],
        'createtime' => $admininfo['createtime'],
        'group_text' => $admininfo['group_text'],
        'openid' => $admininfo['openid'],
      ];

      $this->success('授权登陆成功', '', $data);
    }
  }
 
  // 绑定
  public function bind()
  {
    $username = $this->request->param('username', '', 'trim');
    $password = $this->request->param('password', '', 'trim');
    $code = $this->request->param('code', '', 'trim');

    $admininfo = $this->AdminModel->where('username', $username)->find();

    if (!$admininfo) {
      $this->success('当前管理员不存在');
    }

    // 密码加密
    $password = md5(md5($password) . $admininfo['salt']);

    if ($password != $admininfo['password']) {
      $this->success('帐号密码错误');
    }

    $openid = $this->code2Session($code);

    if (!$openid) {
      $this->error('授权登陆失败');
    }

    $result = $this->AdminModel->where('id', $admininfo['id'])->update(['openid' => $openid]);

    if ($result) {
      $data = [
        'id' => $admininfo['id'],
        'username' => $admininfo['username'],
        'nickname' => $admininfo['nickname'],
        'avatar' => $admininfo['avatar_text'],
        'email' => $admininfo['email'],
        'mobile' => $admininfo['mobile'],
        'createtime' => $admininfo['createtime'],
        'group_text' => $admininfo['group_text'],
        'openid' => $openid,
      ];

      $this->success('帐号绑定成功', '', $data);
    } else {
      $this->error('帐号绑定失败');
    }
  }


  // 修改个人资料
  public function profile()
  {
    $adminid = $this->request->param('adminid', 0, 'trim');
    $params = $this->request->param();

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->success('当前管理员不存在');
    }

    $data = [
      'id' => $adminid,
      'nickname' => $params['nickname'],
      'mobile' => $params['mobile'],
      'email' => $params['email'],
    ];
    // var_dump($params);exit;
    if (isset($params['password']) && !empty($params['password']) && $params['password'] != "[object Undefined]") {
      $data['password'] = md5(md5($params['password']) . $admininfo['salt']);
    }

    // 判断是否修改头像
    if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
      $res = upload('avatar');
      if ($res['code'] == 0) {
        $this->error($res['msg']);
      }
      $data['avatar'] = $res['data'];
    }

    $result = $this->AdminModel->isUpdate()->save($data);

    if ($result === false) {
      if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
        is_file(trim($data['avatar'], '/')) && unlink(trim($data['avatar'], '/'));
      }
      $this->error('修改资料失败');
    } else {
      if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
        is_file(trim($admininfo['avatar'], '/')) && unlink(trim($admininfo['avatar'], '/'));
      }

      $info = $this->AdminModel->find($adminid);

      $respones = [
        'id' => $info['id'],
        'username' => $info['username'],
        'nickname' => $info['nickname'],
        'avatar' => $info['avatar_text'],
        'email' => $info['email'],
        'mobile' => $info['mobile'],
        'createtime' => $info['createtime'],
        'group_text' => $info['group_text'],
        'openid' => $admininfo['openid'],
      ];

      $this->success('修改成功', '', $respones);
    }
  }

  // 微信解绑
  public function wechat()
  {
    $adminid = $this->request->param('adminid', 0, 'trim');

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->success('当前管理员不存在');
    }

    $result = $this->AdminModel->where('id', $adminid)->update(['openid' => null]);

    if ($result) {
      $data = [
        'id' => $admininfo['id'],
        'username' => $admininfo['username'],
        'nickname' => $admininfo['nickname'],
        'avatar' => $admininfo['avatar_text'],
        'email' => $admininfo['email'],
        'mobile' => $admininfo['mobile'],
        'createtime' => $admininfo['createtime'],
        'group_text' => $admininfo['group_text'],
        'openid' => null,
      ];
      $this->success('微信解绑成功', '', $data);
    } else {
      $this->error('微信解绑失败');
    }
  }

  //微信授权登录
  public function code2Session($code = null)
  {

    // AppSecret(小程序密钥)  填自己的
    // $secret = "0bfede4da91a939ecdf05f37662cd80f";
    $secret = "dec1d0d5a7255ff44b798246fe9cc9e2";


    // AppID(小程序ID) 填自己的
    // $appid = "wx2d0b2bf122c3303d";
    $appid = "wx4889e4259e814fe3";

    // 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";


    $result = HttpRequest($url, '', 0);

    return $result['openid'];
  }

  //h5登录
  public function loginh5()
  {
    $adminid = $this->request->param('adminid', 0, 'trim');
    $params = $this->request->param();


    $data = [
      'username' => $params['username'],
      'password' => ''
    ];

    $salt =  $this->AdminModel->where('username', $params['username'])->value('salt');
    $data['password'] = md5(md5($params['password']) . $salt);


    $info =  $this->AdminModel->where($data)->find();

    if ($info) {
      $respones = [
        'id' => $info['id'],
        'username' => $info['username'],
        'nickname' => $info['nickname'],
        'avatar' => $info['avatar_text'],
        'email' => $info['email'],
        'mobile' => $info['mobile'],
        'createtime' => $info['createtime'],
        'group_text' => $info['group_text'],
        // 'openid' => $admininfo['openid'],
      ];
    }else{
      $this->error('登录失败，请检查是否有误');
    }
    $this->success('登录成功','',$respones);
    // return $respones;
  }
}
